<template>
	<view>
		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item, i) in goodsInfo.pics" :key="i">
				<image :src="item.pics_big" mode="aspectFit" @click="onPreviewImage(i)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区域 -->
		<view class="goods-info-box">
			<!-- 商品价格 -->
			<view class="price" v-if="goodsInfo.goods_price">￥{{goodsInfo.goods_price}}</view>
			<!-- 信息主体区域 -->
			<view class="goods-info-body">
				<!-- 商品名称 -->
				<view class="goods-name">{{goodsInfo.goods_name}}</view>
				<!-- 收藏 -->
				<view class="favi">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!-- 运费 -->
			<view class="yf">快递：免运费</view>
			<!-- 商品详情 -->
			<!-- <rich-text :nodes="goodsInfo.goods_introduce"></rich-text> -->
			<div v-html="goodsInfo.goods_introduce"></div>
		</view>
		<uni-section title="基础用法" type="line" class='section'>
			<uni-goods-nav @click="onClick" @buttonClick='onButtonClick' :options="options" />
		</uni-section>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'
	export default {
		data() {
			return {
				goodsInfo: {},
				options: [{
					icon: 'shop',
					text: '店铺',
					info: 2,
					infoBackgroundColor: '#007aff',
					infoColor: "#f5f5f5"
				}, {
					icon: 'cart',
					text: '购物车',
					info: 2
				}],
			}
		},
		computed: {
			...mapState('cartModule', ['cart']),
			...mapGetters('cartModule', ['goodsTotal'])
		},
		watch:{
			goodsTotal:{
				handler(val){
					const findResult = this.options.find((x) => x.text === '购物车')
					findResult.info = val
				},
				immediate:true
			}
		},
		onLoad(options) {
			const id = options.goods_id
			this.getGoodsInfo(id)
			console.log(1111111, this.goodsTotal);
		},
		methods: {
			...mapMutations('cartModule', ['addToCart']),
			async getGoodsInfo(goods_id) {
				const {
					data
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id
				})
				if (data.meta.status !== 200) {
					return uni.showToast({
						title: '数据请求失败！',
						duration: 1500,
						icon: 'none',
					})
				}
				data.message.goods_introduce = data.message.goods_introduce.replace(/<img /g,
					'<img style="vertical-align: middle;"')
				this.goodsInfo = data.message
				console.log(data);
			},
			onPreviewImage(i) {
				console.log(i);
				console.log(this.goodsInfo.pics);
				let arr = this.goodsInfo.pics.map(item => item.pics_big)
				uni.previewImage({
					current: i,
					urls: arr
				})
			},
			onClick(e) {
				console.log(1111, e);
				if (e.content.text === '购物车') {
					uni.switchTab({
						url: '/pages/cart/cart'
					})
				}
			},
			onButtonClick(e) {
				console.log(1111, this.goodsTotal);
				if (e.content.text === '加入购物车') {
					const goods = {
						goods_id: this.goodsInfo.goods_id, // 商品的Id
						goods_name: this.goodsInfo.goods_name, // 商品的名称
						goods_price: this.goodsInfo.goods_price, // 商品的价格
						goods_count: 1, // 商品的数量
						goods_small_logo: this.goodsInfo.goods_small_logo, // 商品的图片
						goods_state: true // 商品的勾选状态
					}
					this.addToCart(goods)
				}
			}
		}
	}
</script>

<style lang="scss">
	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	// 商品信息区域的样式
	.goods-info-box {
		padding: 10px;
		padding-right: 0;

		.price {
			color: #c00000;
			font-size: 18px;
			margin: 10px 0;
		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
				padding-right: 10px;
			}

			// 收藏区域
			.favi {
				width: 120px;
				font-size: 12px;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				border-left: 1px solid #efefef;
				color: gray;
			}
		}

		// 运费
		.yf {
			margin: 10px 0;
			font-size: 12px;
			color: gray;
		}
	}

	.section {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
	}
</style>
